--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--

require "UnLua"
local Screen = require "Screen"
local Debug = require "Debug"
local Const = require "Const"

local M = Class()

function M:UserConstructionScript()
	self.ForwardVec = UE.FVector()
	self.RightVec = UE.FVector()
	self.UpVec = UE.FVector()
	self.ControlRot = UE.FRotator()

	self.BaseTurnRate = 45.0
	self.BaseLookUpRate = 45.0

  self.MainUI = nil

  -- self.bUseRootMotion = true
  self.bUseRootMotion = false

  self.Ch_Class = UE.UClass.Load(Const.CBP_Class)
  -- self.Ch_Class = UE.UClass.Load(Const.CBP_MAP_Class)

end

function M:ReceiveBeginPlay()
  if self:IsLocalPlayerController() then
		self.MainUI = UE.UWidgetBlueprintLibrary.Create(self, UE.UClass.Load(Const.Main_UI_Class))
		self.MainUI:AddToViewport()
	end

	self.Overridden.ReceiveBeginPlay(self)
end


function M:UpdateHp(Value)
  self.MainUI.HpBar:SetPercent(Value)
end

function M:Turn(AxisValue)
	self:AddYawInput(AxisValue)
end

function M:TurnRate(AxisValue)
	local DeltaSeconds = UE.UGameplayStatics.GetWorldDeltaSeconds(self)
	local Value = AxisValue * DeltaSeconds * self.BaseTurnRate
	self:AddYawInput(Value)
end

function M:LookUp(AxisValue)
	self:AddPitchInput(AxisValue)
end

function M:LookUpRate(AxisValue)
	local DeltaSeconds = UE.UGameplayStatics.GetWorldDeltaSeconds(self)
	local Value = AxisValue * DeltaSeconds * self.BaseLookUpRate
	self:AddPitchInput(Value)
end

function M:MoveForward(AxisValue)
	if self.Pawn then
    if self.bUseRootMotion then
      local Ch = self.Pawn:Cast(self.Ch_Class)
      if Ch then
        local MoveAxis = Ch.MoveAxis
        if MoveAxis then
          Ch.MoveAxis.Y = AxisValue
          -- Screen.Print(string.format("x=%f, y=%f", MoveAxis.X, MoveAxis.Y))
        end
      end
    else
      local Rotation = self:GetControlRotation(self.ControlRot)
      Rotation:Set(0, Rotation.Yaw, 0)
      local Direction = Rotation:ToVector(self.ForwardVec)		-- Rotation:GetForwardVector()
      self.Pawn:AddMovementInput(Direction, AxisValue)
    end
  end

end

function M:MoveRight(AxisValue)
	if self.Pawn then
    if self.bUseRootMotion then
      local Ch = self.Pawn:Cast(self.Ch_Class)
      if Ch then
        local MoveAxis = Ch.MoveAxis
        if MoveAxis then
          Ch.MoveAxis.X = AxisValue
          -- Screen.Print(string.format("x=%f, y=%f", MoveAxis.X, MoveAxis.Y))
        end
      end
    else
      local Rotation = self:GetControlRotation(self.ControlRot)
      Rotation:Set(0, Rotation.Yaw, 0)
      local Direction = Rotation:GetRightVector(self.RightVec)
      self.Pawn:AddMovementInput(Direction, AxisValue)
    end
	end

end

function M:MoveUp(AxisValue)
	if self.Pawn then
		local Rotation = self:GetControlRotation(self.ControlRot)
		Rotation:Set(0, Rotation.Yaw, 0)
		-- local Direction = Rotation:ToVector(self.UpVec)		-- Rotation:GetUpVector()
		local Direction = Rotation:GetUpVector()
		-- self.Pawn:AddMovementInput(Direction, AxisValue)
	end
end

------

function M:Jump_Pressed()
	if self.Pawn then
    local CBP = self.Pawn:Cast(self.Ch_Class)
    if CBP then
      CBP:Jump()
    end
	end
end

------

function M:Fire_Pressed()
	if self.Pawn then
		self.Pawn:StartFire_Server()
	else
    Screen.Print("PC: no pawn")
		-- UE.UKismetSystemLibrary.ExecuteConsoleCommand(self, "RestartLevel")
	end
end

function M:Fire_Released()
	if self.Pawn then
		self.Pawn:StopFire_Server()
	end
end


--------

function M:P_Pressed()
	if self.Pawn then
    local AC = self.Pawn.AttrComp
    -- Screen.Print(string.format("Hp: %d/%d", AC.Health, AC.MaxHealth))
    Debug.Print(self.Pawn)
    Debug.Print(self.UpVec)
    Debug.Print(self.ControlRot)
  end
end

function M:Tab_Pressed()
  Screen.Print("PC: R ..")
	if self.Pawn then
    self.Pawn.bIsRunning = not self.Pawn.bIsRunning
  end
end


return M
